package com.jof.framework.jdbc;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

/**
 * 封装jdbc的监控对象信息
 * @author Jonathan
 *
 */
public class JdbcLogContext implements Serializable {
	/**
	 * statement语句
	 */
	public static final int STATEMENT = 0;
	/**
	 * 预处理语句
	 */
	public static final int PREPARED_STATEMENT = 1;
	private static final long serialVersionUID = -5954369513116833831L;
	/**
	 * 执行开始时间(ms)
	 */
	private long startTime;
	/**
	 * 执行结束时间(ms)
	 */
	private long endTime;
	/**
	 * sql语句类型(statement,preparedStatement..)
	 */
	private int type;
	/**
	 * 执行的sql语句
	 */
	private StringBuilder sql;
	/**
	 * sql参数值
	 */
	private List<ParameterValue> parameters = new LinkedList<ParameterValue>();
	/**
	 * 异常堆栈信息
	 */
	private Throwable exception;
	public long getStartTime() {
		return startTime;
	}
	public void setStartTime(long startTime) {
		this.startTime = startTime;
	}
	public long getEndTime() {
		return endTime;
	}
	public void setEndTime(long endTime) {
		this.endTime = endTime;
	}
	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
	}
	public StringBuilder getSql() {
		return sql;
	}
	public void setSql(StringBuilder sql) {
		this.sql = sql;
	}
	
	public Throwable getException() {
		return exception;
	}
	public void setException(Throwable exception) {
		this.exception = exception;
	}
	/**
	 * 释放被占用的对象
	 */
	public void clear(){
		this.parameters.clear();
	}
	/**
	 * 添加sql参数
	 * @param parameter
	 * @param value
	 */
	public void addParameterValue(Object parameter,Object value){
		this.parameters.add(new ParameterValue(parameter,value));
	}
	
	public String toString() {
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append("startTime=");
		stringBuilder.append(startTime);
		stringBuilder.append(",endTime=");
		stringBuilder.append(endTime);
		stringBuilder.append(",type=");
		stringBuilder.append(type);
		stringBuilder.append(",sql=");
		stringBuilder.append(sql);
		stringBuilder.append(",parameters={");
		for(ParameterValue parameterValue : this.parameters){
			stringBuilder.append(parameterValue);
			stringBuilder.append(",");
		}
		stringBuilder.append("},exception=");
		stringBuilder.append(exception);
		return stringBuilder.toString();
	}
}
